package com.ugoodtech.mdcc.client.service.impl;

import com.google.common.collect.Lists;
import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.AllDepartmentListVO;
import com.ugoodtech.mdcc.client.service.DepartmentService;
import com.ugoodtech.mdcc.core.domain.Department;
import com.ugoodtech.mdcc.core.domain.QDepartment;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.domain.UserProfile;
import com.ugoodtech.mdcc.core.repository.DepartmentRepository;
import com.ugoodtech.mdcc.core.repository.UserProfileRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Resource
    private UserProfileRepository userProfileRepository;


    @Override
    public List<Department> getFirstDepartments() {
        QDepartment qDepartment = QDepartment.department;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(builder.and(qDepartment.deleted.eq(false)));
        builder.and(builder.and(qDepartment.parent.isNull()));
        Page<Department> page = departmentRepository.findAll(builder, new PageRequest(0,1000));
        if(null!=page.getContent()){
            return page.getContent();
        }else{
            return new ArrayList<>();
        }
    }

    @Override
    public List<Department> getChildDepartments(Long parentId) {
        QDepartment qDepartment = QDepartment.department;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(builder.and(qDepartment.deleted.eq(false)));
        builder.and(builder.and(qDepartment.parent.id.eq(parentId)));
        Page<Department> page = departmentRepository.findAll(builder, new PageRequest(0,10000));
        if(null!=page.getContent()){
            return page.getContent();
        }else{
            return new ArrayList<>();
        }
    }
    /**
     * this method is provide for "添加人员列表" module.
     **/
    @Override
    public List<AllDepartmentListVO> getAllDepartmentsList(Integer departmentType, String departmentName, User currentUser){
        //        QDepartment qDepartment = QDepartment.department;
//        BooleanBuilder builder = new BooleanBuilder();
        if (currentUser != null) {
            UserProfile userProfile = userProfileRepository.findByUserId(currentUser.getId());
            if (userProfile!=null){
                List<Department> searchDepartmentList;
                List<Department> departmentList = userProfile.getDepartmentList();
                if (!StringUtils.isEmpty(departmentName)){
                    searchDepartmentList =new ArrayList<>();
                    for (Department department : departmentList) {
                        if (department.getName().contains(departmentName)){
                            searchDepartmentList.add(department);
                        }
                    }
                }else{
                    searchDepartmentList = departmentList;
                }
                List<AllDepartmentListVO> allDepartmentListVoList =new ArrayList<>();
                for (Department department : searchDepartmentList) {
                    AllDepartmentListVO allDepartmentListVO =new AllDepartmentListVO();
                    allDepartmentListVO.setDepartmentId(department.getId());
                    allDepartmentListVO.setDepartmentName(department.getName());
                    Department parent = department.getParent();
                    if (parent != null) {
                        allDepartmentListVO.setParentDepartmentId(parent.getId());
                    }
                    allDepartmentListVoList.add(allDepartmentListVO);
                }
                return allDepartmentListVoList;
            }
        }
        return new ArrayList<>();
//        if (departmentName != null) {
//            builder.and(qDepartment.name.like("%" + departmentName + "%"));
//        }
//        Iterable<Department> all = departmentRepository.findAll(builder);
//        DepartmentExample example = new DepartmentExample();
//        DepartmentExample.Criteria criteria = example.createCriteria();
//        criteria.andIsDeletedEqualTo(0);
//        if(departmentType != 3){
//            criteria.andDepartmentTypeEqualTo(departmentType);
//            if(departmentType == 1){
//                if(Objects.nonNull(departmentName) && StringUtils.isNoneEmpty(departmentName)){
//                    criteria.andNameLike("%" + departmentName + "%");
//                }
//            }
//        }

    }
}
